const axios = require("axios");
const tokenurl = "https://aip.baidubce.com/oauth/2.0/token?";
const keywordurl =
  "https://aip.baidubce.com/rpc/2.0/nlp/v1/txt_keywords_extraction?access_token=";
const qs = require("querystring");
const result = require("@/utils/handle");
const param = qs.stringify({
  grant_type: "client_credentials",
  client_id: process.env.BAIDU_CLIENT_ID,
  client_secret: process.env.BAIDU_CLIENT_SECRET,
});

const aiKeyword = async (text) => {
  try {
    let token = await axios.get(tokenurl + param);
    let res = await axios({
      url: keywordurl + token.data.access_token,
      method: "post",
      data: {
        text: [text],
        num: 10,
      },
    });
    if (res.data.results) {
      return res.data.results;
    } else {
      return [];
    }
  } catch (error) {
    throw new result(error, 500);
  }
};

module.exports = aiKeyword;
